package com.tg.app.activity.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.appbase.custom.base.AppVersionBean;
import com.appbase.custom.constant.UserProtocolConstants;
import com.base.BaseActivity;
import com.demo.view.SettingItemTextView;
import com.tange.base.toolkit.AppUtil;
import com.tg.app.R;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.TGGlobalConfigHelper;
import com.tg.app.util.UriUtil;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.util.GoogleUtils;

public class AboutActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvVersionTips;
    private String versionStr;

    private SettingItemTextView policytv;
    private SettingItemTextView agreementtv;
    private SettingItemTextView aboutMe;
    private SettingItemTextView aboutContact;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_about);
        initView();
        hideActionBar();// 隐藏ActionBar
        getVersionData();
    }


    private void showAboutContact(boolean show){
        aboutContact.setVisibility(show? View.VISIBLE : View.GONE);
    }

    private void showAboutMe(boolean show){
        aboutMe.setVisibility(show? View.VISIBLE : View.GONE);
    }



    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    protected void initView() {

        tvVersionTips = findViewById(R.id.tv_about_version_tips);
        findViewById(R.id.back_toolbar).setOnClickListener(this);

        TextView tvTitle = findViewById(R.id.device_name);
        findViewById(R.id.rl_about_layout).setOnClickListener(this);

        String title = String.format(getString(R.string.about_title), getString(R.string.app_name));

        tvTitle.setText(title);

        TextView tvVersion = findViewById(R.id.tv_about_version);
        versionStr = AppUtil.getVersionName(this);
        String version = String.format(getString(R.string.about_version), versionStr);
        tvVersion.setText(version);

        aboutContact = findViewById(R.id.contact_us);
        aboutContact.setOnClickListener(this);

        aboutMe = findViewById(R.id.about_me);
        aboutMe.setOnClickListener(this);


        policytv = findViewById(R.id.policytv);
        policytv.setOnClickListener(this);

        agreementtv = findViewById(R.id.agreementtv);
        agreementtv.setOnClickListener(this);

        showAboutMe(TGGlobalConfigHelper.getInstance().isShow_about_us() && GoogleUtils.isGoogleChannel());
        showAboutContact(TGGlobalConfigHelper.getInstance().isShow_contact_us() && GoogleUtils.isGoogleChannel());
        modifyToolBar();
    }

    private void showVersionUpdateDialog(final AppVersionBean version) {
        int localVersion = AppUtil.getVersionCode(getBaseContext());
        if (localVersion < version.getVersion_no()) {
            tvVersionTips.setText(R.string.newtip);
            tvVersionTips.setTextColor(getResources().getColor(R.color.white));
            tvVersionTips.setBackgroundResource(R.drawable.shape_about_version_new);
            if (!GoogleUtils.isGoogleChannel()){
            //    AppUpgradeManager.showUpgradeDialog(this, version);
            }

        } else {
            String msg = getString(R.string.about_version_lastest);
            tvVersionTips.setTextColor(getResources().getColor(R.color.about_version_normal_color));
            tvVersionTips.setBackgroundColor(getResources().getColor(R.color.white));
            tvVersionTips.setText(msg);
            showToast(msg);
        }
    }



    private void getVersionData() {
//        AppUpgradeManager.requestUpgrade(new AppUpgradeRequestListener() {
//            @Override
//            public void onSuccess(AppVersionBean appVersionBean) {
//                if (appVersionBean != null) {
//                    showVersionUpdateDialog(appVersionBean);
//                }
//            }
//
//            @Override
//            public void onFailed(String msg) {
//                showToast(msg);
//            }
//        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back_toolbar) {
            finish();
        } else if (id == R.id.rl_about_layout) {
            if (!GoogleUtils.isGoogleChannel()){
                getVersionData();
            }else {
                AppUtil.launchGooglePlay(getBaseContext());
            }

        } else if (id == R.id.agreementtv) {
            ActivityHelper.getAgreementForWebView(this, UserProtocolConstants.USER_PROTOCOL);
        } else if (id == R.id.policytv) {
            ActivityHelper.getAgreementForWebView(this, UserProtocolConstants.PRIVACY);
        } else if (id == R.id.about_me){
            onClickAboutMe();
        } else if (id == R.id.contact_us){
            onClickAboutContact();
        }
    }


    private void onClickAboutMe(){
        UriUtil.openServePlayImpl(AboutActivity.this, new Intent(), TGGlobalConfigHelper.getInstance().getAbout_us_link(), ResourcesUtil.getString(R.string.about_demo), 0);
    }

    private void onClickAboutContact(){
        UriUtil.openServePlayImpl(AboutActivity.this, new Intent(), TGGlobalConfigHelper.getInstance().getContact_us_link(), ResourcesUtil.getString(R.string.about_contactus), 0);
    }

}
